package com.zhang.repository;

import com.zhang.data_object.AlbumDO;
import com.zhang.util.DBUtil;
import com.zhang.util.Log;
import com.zhang.view_object.AlbumVO;
import com.zhang.view_object.publish.AlbumView;
import com.zhang.view_object.publish.PlayAlbumView;
import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author happy
 **/
public class AlbumRepo {
    @SneakyThrows
    public AlbumDO selectOneByUidAndAid(Integer uid, int aid) {
        String sql = "select aid,uid,title,cover,state from albums where aid = ? and uid = ?";
        try(Connection c = DBUtil.connection()){
            try(PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,aid);
                ps.setInt(2,uid);
                Log.println("执行SQL"+sql);
                try(ResultSet rs = ps.executeQuery()) {
                    if (!rs.next()){
                        return null;
                    };
                    return new AlbumDO(
                            rs.getInt("aid"),
                            rs.getInt("uid"),
                            rs.getString("title"),
                            rs.getString("cover"),
                            rs.getInt("state")
                    );
                }
            }
        }
    }

    @SneakyThrows
    public void insert(Integer uid, String title, String cover, int state) {
        String sql = "insert into albums (uid,title,cover,state) values(?,?,?,?)";
        try(Connection c = DBUtil.connection()){
            try(PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setInt(1,uid);
                ps.setString(2,title);
                ps.setString(3,cover);
                ps.setInt(4,state);
                ps.executeUpdate();
                Log.println("执行SQL:"+ps);
            }
        }
    }

    @SneakyThrows
    public List<AlbumVO> selectAlbumsUid(Integer uid) {
        List<AlbumVO> list = new ArrayList<>();
        String sql = "select aid,title,cover,state from albums where uid = ? order by aid desc";
        try (Connection c = DBUtil.connection()){
            try (PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,uid);
                Log.println("执行SQL:"+ps);
                try(ResultSet rs = ps.executeQuery()){
                    while (rs.next()){
                        AlbumVO albumVO = new AlbumVO(
                                rs.getInt("aid"),
                                rs.getString("title"),
                                rs.getString("cover"),
                                rs.getInt("state")
                        );
                        list.add(albumVO);
                    }
                }
            }
        }
        return list;
    }

    @SneakyThrows
    public List<AlbumView> selectAlbumsAllPublish() {
        List<AlbumView> list = new ArrayList<>();
        String sql = "select aid,username,title,cover from albums a join users u on a.uid = u.uid where state = 2 order by aid desc ";
        try(Connection c = DBUtil.connection()){
            try(PreparedStatement ps = c.prepareStatement(sql)){
                Log.println("执行SQL:"+ps);
                try(ResultSet rs = ps.executeQuery()){
                    while (rs.next()){
                        list.add(new AlbumView(
                                rs.getInt("aid"),
                                rs.getString("username"),
                                rs.getString("title"),
                                rs.getString("cover")
                        ));
                    }
                    return list;
                }
            }
        }
    }

    @SneakyThrows
    public PlayAlbumView selectByAidAndUid(String aid) {
        String sql = "select aid, username, title, cover from albums a join users u on a.uid = u.uid where aid = ?";
        try(Connection c = DBUtil.connection()){
            try(PreparedStatement ps = c.prepareStatement(sql)){
                ps.setString(1,aid);
                Log.println("执行SQL:"+ps);
                try(ResultSet rs = ps.executeQuery()){
                    if (!rs.next()){
                        throw new RuntimeException("专辑不存在");
                    }
                    Log.println("数据库查询结果不为空");
                    return new PlayAlbumView(
                            rs.getInt("aid"),
                            rs.getString("username"),
                            rs.getString("cover"),
                            rs.getString("title")
                    );
                }
            }
        }
    }
}